import {
  Component
} from '@angular/core';
import {
  NavController,
  NavParams,
  ModalController,
  LoadingController,
  AlertController
} from 'ionic-angular';
import {
  ModifyDataPage
} from '../modify-data/modify-data'
import {
  Http,
  Headers,
  RequestOptions
} from '@angular/http';
import {
  AppConfig
} from './../../app/app.config';
import {
  Storage
} from '@ionic/storage'
import {
  baseCommon
} from '../../common/baseCommon';
import { IndexPage } from '../index';
/**
 * Generated class for the PersonalPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@Component({
  selector: 'page-personal',
  templateUrl: 'personal.html',
})
export class PersonalPage{

  personMes: any;
  sessionID: any;
  phone: any;
  listData: any = [];
  ifSuccess:boolean = false;

  constructor(public navCtrl: NavController,
    public navParams: NavParams,
    public modalCtrl: ModalController,
    public http: Http,
    public storage: Storage,
    public alertCtrl:AlertController,
    public loadingCtrl: LoadingController) {
  }

  ionViewWillEnter(){
    this.storage.get('phone').then(val => {
      this.phone = val;
    })

    this.storage.get('sessionID').then(val => {
      baseCommon.getInfo(this.http,this.phone,val)
      .then(data=>{
        this.listData = data;
        this.ifSuccess = true;
      });
      // this.listData = this.listData.json().data;
    })
  }


  /**
   * 更新修改信息
   *
   * @memberof PersonalPage
   */
  goModify() {
    let model = this.modalCtrl.create(ModifyDataPage, {
      listData: this.listData
    });
    model.onDidDismiss(data=>{
      this.storage.get('phone').then(val => {
        this.phone = val;
      })

      this.storage.get('sessionID').then(val => {
        baseCommon.getInfo(this.http,this.phone,val);
      })

      AppConfig.viewCtrl = null;
    })
    model.present();
  }

  /**
   * 退出圈子确认弹窗
   *
   * @memberof PersonalPage
   */
  quitLogin() {
    let prompt = this.alertCtrl.create({
      title: '确定要退出此账号吗？',
      buttons: [{
          text: '我再想想',
          handler: data => {

          }
        },
        {
          text: '确认退出',
          handler: data => {
            this.storage.get('sessionID').then(val=>{
              this.QuitLogin(AppConfig.httpLoc+'/logout?sessionID='+val);
            })
          }
        }
      ]
    });
    prompt.present();
  }

  /**
   * 退出失败
   *
   * @memberof PersonalPage
   */
  quitFailure(){
    let alert = this.alertCtrl.create({
      title: '退出失败!',
      subTitle: '可能是前端程序员太喜欢你了，不想让你退出!如果实在想退出，可以加他QQ：1551407603',
      buttons: ['这就去加']
    });
    alert.present();
  }

  /**
   * 退出登录
   *
   * @param {any} apiUrl
   * @memberof PersonalPage
   */
  QuitLogin(apiUrl) {

    let loading = baseCommon.showLoading(this.loadingCtrl,'退出中....')

    let headers = new Headers({
      'Content-Type': 'application/x-www-form-urlencoded',
    });

    let options = new RequestOptions({
      headers: headers,
    });


    this.http.get(apiUrl, options)
      .toPromise()
      .then(data => {
        console.log(data);
        loading.dismissAll();
        if(data.json().message == '成功'){
          this.storage.clear();
          let model = this.modalCtrl.create(IndexPage);
          model.present();
        }else{
          this.quitFailure();
        }
      }, err => {
        console.log(err);
      })
  }

}
